package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Problem;
import cn.wolfcode.luowowo.article.query.ProblemCommendQuery;
import cn.wolfcode.luowowo.article.service.IProblemService;
import cn.wolfcode.luowowo.comment.domain.Prob;
import cn.wolfcode.luowowo.comment.domain.ProblemComment;
import cn.wolfcode.luowowo.comment.query.ProblemCommentQuery;
import cn.wolfcode.luowowo.comment.service.IProblemCommentService;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class ProblemcommtController {

    @Reference
    private IProblemCommentService problemCommentService;
    @Reference
    private IProblemService problemService;

    //根据问题id查询问题以及问题的回答
    @RequestMapping("/read")  //跳转到显示问题回答页面
    public String read(Model model,Long probId){
       //查问题
        Problem problem = problemService.selectByPrimaryKey(probId);
        model.addAttribute("pro",problem);
        //根据问题id查回答
        List<ProblemComment> comments = problemCommentService.getNewTop10(probId);
        model.addAttribute("comments",comments);
        //查詢回答數
        Long count = problemCommentService.count(probId);
        model.addAttribute("count",count);
       //回答数
        problemService.updateReplynum(probId,count);
        return "wenda/wendaDetail";
    }


    @RequestMapping("/savacommt")
    @ResponseBody
    public Object savacommt(ProblemComment problemComment, @GetUserInfoAnnotation UserInfo userInfo){
        problemComment.setLevel(userInfo.getLevel());
        problemComment.setHeadUrl(userInfo.getHeadImgUrl());
        problemComment.setUserId(userInfo.getId());
        problemComment.setUsername(userInfo.getNickname());
        problemCommentService.sava(problemComment);
       return problemComment.getProbId();
    }


    @RequestMapping("/red") //问答首页
    public String wenda(Model model,ProblemCommendQuery qo,Long rid){
        //问题显示
        PageInfo pageInfo = problemService.query(qo);
        List<Problem> list = pageInfo.getList();
        for (Problem problem : list) {
            List<String> arrlist = problem.getArrlist();
            String tag = problem.getTag();
            String[] split = tag.split(",");
            for (String s : split) {
                arrlist.add(s);
            }
            problem.setArrlist(arrlist);
        }
        model.addAttribute("pageInfo",pageInfo);
        List<Prob> plist =problemCommentService.getshuju(rid);
        model.addAttribute("f",plist);
        return "/wenda/wenda";
    }

}
